// 定义命名空间，保证对象的唯一性，防止冲突
var fosunAPP = fosunAPP || {};

fosunAPP.Global = new function() {
	// 
	var webroot = "";
	// 当前菜单id
	var menuid = "";
	// 当前菜单的pid
	var menupid = "";
	// 当前用户id
	var uid;
	// 当前用户角色id
	var roleid;
	this.getWebroot = function() {
		return this.webroot;
	};
	this.setWebroot = function(_webroot) {
		this.webroot = _webroot;
	}
	this.getMenuid = function() {
		return this.menuid;
	};
	this.setMenuid = function(_menuid) {
		this.menuid = _menuid;
	}
	this.getMenupid = function() {
		return this.menupid;
	};
	this.setMenupid = function(_menupid) {
		this.menupid = _menupid;
	}
	this.getUid = function() {
		return this.uid;
	};
	this.setUid = function(_uid) {
		this.uid = _uid;
	}
	this.getRoleid = function() {
		return this.roleid;
	};
	this.setRoleid = function(_roleid) {
		this.roleid = _roleid;
	}
};


/**
 * 系统失败时的提示
 */
$(document).ajaxError(function() {
			toastr.error("操作失败,请重试或联系系统管理员！");
		});

// override dialog's title function to allow for HTML titles
try {
	$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
						_title : function(title) {
							var $title = this.options.title || '&nbsp;'
							if (("title_html" in this.options)
									&& this.options.title_html == true)
								title.html($title);
							else
								title.text($title);
						}
					}));
} catch (e) {
}

fosunAPP.SysComponents = {
	/**
	 * 根据数据字典一级code获取数据
	 * 
	 * @param pCode
	 * @param comboBoxId:页面组件id
	 */
	getDicts : function(pCode, comboBoxId) {
		$.get(	MedicareAPP.Global.getWebroot()
						+ "/sys/dict.html?action=getDictsByPcode", {
					pCode : pCode
				}, function(data) {
					var _tpl = '<option value="{code}">{displayName}</option>';
					$(data).each(function(index, element) {
								$('#' + comboBoxId).append(nano(_tpl, element));
							})
				});
	},
	/**Id设置默认值
	 * @param posId
	 * @param comboBoxId:页面组件id
	 */
	getPositions : function(posId, comboBoxId,Id) {
		$.get(	MedicareAPP.Global.getWebroot()
						+ "/sys/longtimes/longtimes.html?action=getPositions", {
					posId : posId
				}, function(data) {
					var _tpl = '<option value="{posId}">{name}</option>';
					var _tpled = '<option value="{posId}" selected = "selected">{name}</option>';
					$(data).each(function(index, element) {
						if(Id==element.posId){
							$('#' + comboBoxId).append(nano(_tpled, element));
						}else { $('#' + comboBoxId).append(nano(_tpl, element));}
							})
				
				});
	},
	
	
	
	
	
	/**
	 * 根据数据字典一级code获取数据
	 * 
	 * @param pCode
	 * @param comboBoxId:页面组件id
	 */
	getDictsExt : function(pCode, comboBoxId) {
		$.get(	MedicareAPP.Global.getWebroot()
						+ "/sys/dict.html?action=getDictsByPcode", {
					pCode : pCode
				}, function(data) {
					var _tpl = '<option value="{displayName}">{displayName}</option>';
					$(data).each(function(index, element) {
								$('#' + comboBoxId).append(nano(_tpl, element));
							})
				});
	},
	/**
	 *	将后台时间戳转换成YYYY-mm-DD格式
	 * @param date
	 * @returns {String}
	 * qianhe.du
	 */
	formatDate : function(date) {
		var d = new Date(date);
		var month = d.getMonth() + 1;
		var day = d.getDate();
		if (Number(month) < 10) {
			month = "0" + month;
		}
		if (Number(day) < 10) {
			day = "0" + day;
		}
		return d.getFullYear() + "-" + month + "-" +  day;
	},
	/**
	 * 将json对象根据key值设置到form里的组件里
	 * 
	 * @param {}
	 *            data：json对象
	 */
	setValues : function(data) {
		for (var key in data) {
			var _obj = $('#' + key);
			
			if (_obj.attr('type') == 'checkbox') {
				if (data[key] == 1) {
					_obj.prop("checked", "checked");
				}
			}else if(_obj.attr('type') == 'radio'){
				$("input[name="+key+"][value="+data[key]+"]").prop("checked", true);
			}else {
				if (_obj.is('input') || _obj.is('select')) {
					
					_obj.val(data[key]);
				} else if (_obj.is('textarea')) {
					_obj.text((data[key] == null) ? '' : data[key]);
					_obj.val((data[key] == null) ? '' : data[key]);
				}
			}
		}
	},
	/**
	 * 将数组转为字符串，并以split间隔
	 * 
	 * @param array
	 * @param split
	 *            间隔符
	 */
	array2String : function(array, split) {
		if (typeof array == 'undefined') {
			return '';
		}
		var _str = '';
		$.each(array, function(index, element) {
					_str += element + split
				});
		return _str;
	},
	/**
	 * 将字符串转换成数组
	 * 
	 * @param str
	 * @param split
	 * @returns {*}
	 */
	string2array : function(str, split) {
		if ($.trim(str) != '') {
			return str.split(split);
		} else {
			return '';
		}
	},
	showHelpMessage : function(msg, type, title) {
		toastr.clear();
		if (type == 'success') {
			toastr.success(msg, title);
		} else if (type == 'error') {
			toastr.error(msg, title);
		} else if (type == 'warn') {
			toastr.warning(msg, title);//warh修改warning,添加参数title - qianhe.du
		} else {
			toastr.info(msg, title);
		}
	}
}
Date.prototype.format = function(format) {
	var o = {
		"M+" : this.getMonth() + 1,
		// month
		"d+" : this.getDate(),
		// day
		"h+" : this.getHours(),
		// hour
		"m+" : this.getMinutes(),
		// minute
		"s+" : this.getSeconds(),
		// second
		"q+" : Math.floor((this.getMonth() + 3) / 3),
		// quarter
		"S" : this.getMilliseconds()
		// millisecond
	};
	if (/(y+)/.test(format) || /(Y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4
						- RegExp.$1.length));
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1
							? o[k]
							: ("00" + o[k]).substr(("" + o[k]).length));
		}
	}
	return format;
};

$.extend($.fn.fmatter, {
			dateLongFmatter : function(cellvalue, options, rowdata) {
				return (new Date(parseFloat(cellvalue))).format("yyyy-MM-dd hh:mm:ss");;
			},
			dateShortFmatter : function(cellvalue, options, rowdata) {
				return (new Date(parseFloat(cellvalue))).format("Y-m-d");;
			}
		});